package com.jiuyun.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiuyun.entity.JsonResult;
import com.jiuyun.entity.Users;
import com.jiuyun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
* @CrossOrigin
* 支持跨域
* */

@Controller
@CrossOrigin
public class UsersController {
    @Autowired
    UserService userService;
    @GetMapping("/users")
    public String index(ModelMap map,int page){
        System.out.println("进入USersController控制器 ");
        PageHelper.startPage(page,5);
        List list=userService.getAll();
        PageInfo pinfo=new PageInfo(list);

        map.put("pinfo",pinfo);

        return "index";
    }
    @GetMapping("/ajaxusers")
    @ResponseBody
    public JsonResult ajax(){
        System.out.println("进入USersController控制器 ");
        List list=userService.getAll();
        JsonResult ok=new JsonResult(200,"获取列表成功",list);

        return ok;
    }

    @PostMapping("/users")
    @ResponseBody
    public JsonResult add(Users user) {
        int row = userService.add(user);
        return row > 0 ? new JsonResult(200, "添加用户信息成功！！！", null)
                : new JsonResult(404, "添加用户信息失败！！！", null);
    }
    @PutMapping("/users")
    @ResponseBody
    public JsonResult updata(@RequestBody Users user, @RequestParam(required = false) Integer page){
        System.out.println(user);
        int row=userService.update(user);
        return row>0?new JsonResult(200,"获取列表成功",null): new JsonResult(200,"获取列表失败",null);
    }
    @DeleteMapping("/users/{userid}")
    @ResponseBody
    public JsonResult deleteUser(@PathVariable int userid) {
        int row = userService.del(userid);
        return row > 0 ? new JsonResult(200, "删除成功", null) : new JsonResult(200, "删除失败", null);
    }

}
